<?php

namespace App\Http\Controllers\Util;

use App\Model\Attachment;
use App\Model\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UploadController extends Controller
{
    //
    public function upload(Request $request){

        $file = $request->file('file');

        if ($file->isValid()) {

            $path = $file->store(date('ymd'), 'upload');
        }

        $end_path='/upload/'. $path;

        Attachment::create([
            'filename'=>$file->getClientOriginalName(),
            'path' => $end_path,
            'user_id' => auth()->user()->id
         ]);

//        return ['file' =>url ($end_path), 'code' => 0];

          return [
              'code'=>'0',
              'data'=>[
                  'src'=>url($end_path)
              ]

          ];
    }

    public function lists () {
        $data = Attachment::where('user_id',auth ()->user ()->id)->paginate(10);
        $db = $data->toArray();
        foreach($db['data'] as $k=>$v){
            $db['data'][$k]['url'] = $v['path'];
        }
        return [
            'code'=>0,
            'data'=>$db['data'],
            'page'=>$data->links() . ''
        ];
    }
}
